package cn.iocoder.yudao.module.biz.controller.admin.emergencyMaterials;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.emergencyMaterials.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.emergencyMaterials.BizEmergencyMaterialsDO;
import cn.iocoder.yudao.module.biz.convert.emergencyMaterials.BizEmergencyMaterialsConvert;
import cn.iocoder.yudao.module.biz.service.emergencyMaterials.BizEmergencyMaterialsService;

@Api(tags = "管理后台 - 应急物资")
@RestController
@RequestMapping("/biz/emergency-materials")
@Validated
public class BizEmergencyMaterialsController {

    @Resource
    private BizEmergencyMaterialsService emergencyMaterialsService;

    @PostMapping("/create")
    @ApiOperation("创建应急物资")
    @PreAuthorize("@ss.hasPermission('biz:emergency-materials:create')")
    public CommonResult<String> createEmergencyMaterials(@Valid @RequestBody BizEmergencyMaterialsCreateReqVO createReqVO) {
        return success(emergencyMaterialsService.createEmergencyMaterials(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新应急物资")
    @PreAuthorize("@ss.hasPermission('biz:emergency-materials:update')")
    public CommonResult<Boolean> updateEmergencyMaterials(@Valid @RequestBody BizEmergencyMaterialsUpdateReqVO updateReqVO) {
        emergencyMaterialsService.updateEmergencyMaterials(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除应急物资")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:emergency-materials:delete')")
    public CommonResult<Boolean> deleteEmergencyMaterials(@RequestParam("id") String id) {
        emergencyMaterialsService.deleteEmergencyMaterials(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得应急物资")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:emergency-materials:query')")
    public CommonResult<BizEmergencyMaterialsRespVO> getEmergencyMaterials(@RequestParam("id") String id) {
        BizEmergencyMaterialsDO emergencyMaterials = emergencyMaterialsService.getEmergencyMaterials(id);
        return success(BizEmergencyMaterialsConvert.INSTANCE.convert(emergencyMaterials));
    }

    @GetMapping("/list")
    @ApiOperation("获得应急物资列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:emergency-materials:query')")
    public CommonResult<List<BizEmergencyMaterialsRespVO>> getEmergencyMaterialsList(@RequestParam("ids") Collection<String> ids) {
        List<BizEmergencyMaterialsDO> list = emergencyMaterialsService.getEmergencyMaterialsList(ids);
        return success(BizEmergencyMaterialsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得应急物资分页")
    @PreAuthorize("@ss.hasPermission('biz:emergency-materials:query')")
    public CommonResult<PageResult<BizEmergencyMaterialsRespVO>> getEmergencyMaterialsPage(@Valid BizEmergencyMaterialsPageReqVO pageVO) {
        PageResult<BizEmergencyMaterialsDO> pageResult = emergencyMaterialsService.getEmergencyMaterialsPage(pageVO);
        return success(BizEmergencyMaterialsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出应急物资 Excel")
    @PreAuthorize("@ss.hasPermission('biz:emergency-materials:export')")
    @OperateLog(type = EXPORT)
    public void exportEmergencyMaterialsExcel(@Valid BizEmergencyMaterialsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizEmergencyMaterialsDO> list = emergencyMaterialsService.getEmergencyMaterialsList(exportReqVO);
        // 导出 Excel
        List<BizEmergencyMaterialsExcelVO> datas = BizEmergencyMaterialsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "应急物资.xls", "数据", BizEmergencyMaterialsExcelVO.class, datas);
    }

}
